/**
 *
 * @author ace
 * @create 2017/7/27
 */
class DaikaiPanel extends BasePanel {

    private btn_list: eui.RadioButton;
    private btn_record: eui.RadioButton;

    private noneGroup: eui.Label;

    private scroller: eui.Scroller;
    private itemGroup: eui.Group;

    private info: RecordDetailsInfoItem;

    private btn_rule: ButtonEx;
    private btn_refresh: ButtonEx;
    private btn_create: ButtonEx;

    private lab_num: eui.Label;
    private pageTool: PageTool;

    private textTips: TextTips;

    private daikaiList: any[];
    private daikaiHistory: any[];
    private daikaiDetails: any[];

    private timestamp: number = 0;
    private page: number = 1;

    constructor() {
        super();
        this.skinName = "DaikaiPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.textTips = new TextTips(this.btn_rule);
        this.textTips.desc = "" +
            "1.	账号内房卡数达到100张才能使用俱乐部功能。\n" +
            "2.	最多只能同时开启10个空闲房间。\n" +
            "3.	未开始的牌局会在创建60分钟后自动解散，已开始的牌局不受影响";

        this.lab_num.text = `当前房间数量:0/10`;

        this.info.setType(3);

        this.btn_list.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_record.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_refresh.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_create.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.info.btn_reback.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.i.addEventListener(GameEventType.UPDATE_DAIKAI_LIST, this.update, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_DAIKAI_RECORD_LIST, this.updateRecord, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_DAIKAI_DETAILS_LIST, this.updateRecordDetails, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_RECORD_DETAILS_TYPE, this.infoTypeHandler, this);

        this.pageTool.addEventListener(CommonEventType.CHANGED, this.onUpdatePage, this);
    }

    private infoTypeHandler(type: number){
        this.info.setType(type);
    }

    private onUpdatePage() {
        if (this.page != this.pageTool.page) {
            this.page = this.pageTool.page;

            let start: number = (this.page - 1) * GameConst.Count_Page_Record;
            let end: number = start + GameConst.Count_Page_Record - 1;
            gameManager.socketManager.send(32, {
                start: start,
                end: end,
                time: this.timestamp
            });
        }
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_list:
                // if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
                // else return;

                this.skinState = 'list';
                this.clean();
                gameManager.socketManager.send(31, {type: GameConfig.gameType});
                break;
            case this.btn_record:
                // if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
                // else return;

                this.skinState = 'record';
                this.clean();

                this.timestamp = 0;
                this.page = 1;
                this.pageTool.setPage(this.page);

                gameManager.socketManager.send(32, {
                    start: 0,
                    end: GameConst.Count_Page_Record - 1,
                    time: this.timestamp
                });
                break;
            case this.info.btn_reback:
                this.skinState = 'record';
                this.clean();

                let start: number = (this.page - 1) * GameConst.Count_Page_Record;
                let end: number = start + GameConst.Count_Page_Record - 1;
                gameManager.socketManager.send(32, {
                    start: start,
                    end: end,
                    time: this.timestamp
                });
                break;
            case this.btn_refresh:
                if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
                else return;

                gameManager.socketManager.send(31, {type: GameConfig.gameType});
                break;
            case this.btn_create:
                if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
                else return;

                if (game.player.cur < 100) {
                    EffectTextUtils.showTips(GameConfig.errorCodes[21]);
                    return;
                }

                game.isDaikai = true;
                gameManager.open('CreatePanel');
                break;
        }
    }

    update(list: any[]) {
        if (!list) return;

        this.clean();

        console.log(list);

        this.daikaiList = list;

        let num: number = 0;

        let item: DaikaiItem;
        for (let i: number = 0; i < list.length; i++) {
            item = new DaikaiItem();
            this.itemGroup.addChild(item);

            item.update('list', list[i]);
            item.rank = i + 1;

            if (list[i].status != "start") {
                num++;
            }
        }

        this.lab_num.text = `当前空闲房间数量:${num}/10`;

        this.none = list.length == 0;
    }

    updateRecord(data: any) {

        this.skinState = 'record';

        if (data.hasOwnProperty('zong_shu')) {
            this.pageTool.setScope(1, Math.ceil(+data.zong_shu / GameConst.Count_Page_Record));
        }

        let list: any[] = [];

        if (data.hasOwnProperty('return_data')) {
            list = data.return_data;
        }
        else {
            list = data;
        }

        if (!list) return;

        this.clean();

        if (this.timestamp == 0 && list.length > 0) {
            this.timestamp = list[0].time;
        }

        this.daikaiHistory = list;

        let item: DaikaiItem;
        for (let i: number = 0; i < list.length; i++) {
            item = new DaikaiItem();
            this.itemGroup.addChild(item);

            item.update('record', list[i]);
            item.rank = i + 1 + (this.page - 1) * GameConst.Count_Page_Record;
        }

        this.none = list.length == 0;
    }

    updateRecordDetails(list: any[]): void {

        this.skinState = 'details';

        if (!list) return;

        this.clean();

        this.daikaiDetails = list;

        let item: DaikaiItem;
        for (let i: number = 0; i < list.length; i++) {
            item = new DaikaiItem();
            this.itemGroup.addChild(item);

            item.update('details', list[i]);
            item.rank = i + 1;
        }

        this.none = list.length == 0;
    }

    updateRecordDetailsInfo(rank, roomid, time) {
        this.info.rank = rank;
        this.info.roomid = roomid;
        this.info.time = time;
    }

    clean() {
        this.scroller.stopAnimation();
        this.itemGroup.removeChildren();
        this.itemGroup.scrollV = 0;
    }

    show() {
        super.show();

        this.skinState = 'list';

        this.btn_list.selected = true;

        gameManager.socketManager.send(31, {type: GameConfig.gameType});
    }

    set none(v: boolean) {
        this.noneGroup.visible = v;
    }
}